-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWidth.cpp
More file actions
34 lines (33 loc) · 1.3 KB
/
Width.cpp
File metadata and controls
34 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Width.h"
#include <stdio.h>
int Width(BiTree T)
{ // 求二叉树T的最大宽度
if(T==NULL)
return 0; // 空二叉树的宽度为0
else
{
BiTree Q[100]; // Q是队列,元素为二叉树结点指针,容量足够大
int front=1; // 头指针
int rear=1; // 尾指针
int last=1; // 同层最右结点在队列中的位置
int temp=0; // 局部宽度
int maxw=0; // 最大宽度
Q[rear]=T; // 根结点入队
while(front<=last)
{
BiTree p=Q[front++];
temp++; // 同层元素加1
if(p->lchild!=NULL)
Q[++rear]=p->lchild; // 左孩子入队
if(p->rchild!=NULL)
Q[++rear]=p->rchild; // 右孩子入队
if(front>last) // 一层结束
{
last=rear; // last指向下层最右元素
if(temp>maxw) maxw=temp; // 更新最大宽度
temp=0;
} // if
} // while
return maxw;
}
}